package com.studentapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.gson.Gson;
import com.studentapp.R;
import com.studentapp.bean.LoginGsonBean;
import com.studentapp.tool.MyURL;
import com.studentapp.tool.StudentSpTool;
import com.studentapp.tool.VolleyTool;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar toolbar_login;
    private ImageView iv_img;
    private EditText ed_username;
    private EditText ed_password;
    private Button btn_login;
    private Button btn_register;
    private final Context context = LoginActivity.this;
    private final static String LOGIN_URL = "/user/login";
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        toolbar_login = (Toolbar) findViewById(R.id.toolbar_login);

        //toolbar相关
        setSupportActionBar(toolbar_login);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar_login.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        iv_img = (ImageView) findViewById(R.id.iv_img);
        ed_username = (EditText) findViewById(R.id.ed_username);
        ed_password = (EditText) findViewById(R.id.ed_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);

        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                        if (submit()) {
                            startActivity(intent);
                        } else {
                            Log.d(TAG, "onClick: "+"登录失败");
                        }
                    }
                }.start();
                break;
            case R.id.btn_register:
                Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent1);
                break;
        }
    }

    private Boolean submit() {
        // validate
        String username = ed_username.getText().toString().trim();
        String password = ed_password.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            } else {
                //登录验证
            }
        }

       return sendLoginRequest(username, password);
    }

    /**
     * 发送登录请求的方法
     *
     * @param username 用户名
     * @param password 密码
     */
    private Boolean sendLoginRequest(final String username, final String password) {
        //构建URL
        String url = MyURL.DJP_URL + LOGIN_URL + "?name=" + username + "&password=" + password;

        //调用Volley发送请求并获得返回的json数据
        String postResponse = VolleyTool.getPostResponse(context, url);

        //构建Gson并使用Gson解析数据
        Gson gson = new Gson();
        final LoginGsonBean loginGsonBean = gson.fromJson(postResponse, LoginGsonBean.class);

        //若登录成功,就将当前用户的数据存到sp中
        if (loginGsonBean.isSuccess()) {
            StudentSpTool studentSpTool = new StudentSpTool(context);
            studentSpTool.putStudentBean(loginGsonBean.getData());
        }

        return loginGsonBean.isSuccess();
    }
}
